<template>
  <div>
    <div v-for="(item, index) in chapterInfo" :key="index">
      <div v-if="item.hasChildren">
        <div class="c-pv20 c-pl28 c-pr20">
          <div class="c-flex-row c-aligni-center">
            <div @click="changeShow(index,item)" class="theme-fc c-fs26 c-fw600 iconfont c-textAlign-c">{{item.isShow ? '&#xe8a0;' : '&#xe8a1;'}}</div>
            <div @click="chapterPractice(item)" class="c-flex-row c-aligni-center c-justify-sb c-flex-grow1 c-ml20">
              <div class="c-fs24 c-fc-xblack c-ww200 c-text-ellipsis1">{{item.name}}</div>
              <div class="c-flex-row c-aligni-center">
                <div class="c-fs20 c-fc-gray c-flex-row c-aligni-center c-mr10"><span>{{isWrong?item.hasQuestionNum:item.answerTotal+'/'+item.hasQuestionNum+'道'}}</span><span class="c-ph10 c-fc-sgray" v-if="chapterInfo.lastRid==item.rId">|</span><span class="c-fs20 theme-fc" v-if="chapterInfo.lastRid==item.rId">上次做到</span></div>
                <div class="c-fs30 c-fc-sgray iconfont">&#xe635;</div>
              </div>
            </div>
          </div>
        </div>
        <div v-show="(item.isShow == true&&item.children)" v-for="(cItem, cIndex) in item.children" :key="cIndex">
          <div @click="chapterPractice(cItem)" class="c-flex-row c-aligni-center c-justify-sb c-pv20 c-pl74 c-pr20">
            <div class="c-ww300">
              <div class="c-fs24 c-fc-xblack c-ww300 c-text-ellipsis1">{{cItem.name}}</div>
            </div>
            <div class="c-flex-row c-aligni-center">
              <div class="c-fs20 c-fc-gray c-flex-row c-aligni-center c-mr10"><span>{{isWrong?cItem.hasQuestionNum:cItem.answerTotal+'/'+cItem.hasQuestionNum+'道'}}</span><span class="c-ph10 c-fc-sgray" v-if="chapterInfo.lastRid==cItem.rId">|</span><span class="c-fs20 theme-fc" v-if="chapterInfo.lastRid==cItem.rId">上次做到</span></div>
              <div class="c-fs30 c-fc-sgray iconfont">&#xe635;</div>
            </div>
          </div>
        </div>
      </div>
      <div @click="chapterPractice(item)" v-else-if="!item.hasChildren" class="c-pv20 c-flex-row c-aligni-center c-justify-sb c-pl28 c-pr20">
        <div class="c-ww300">
          <div class="c-fs24 c-fc-xblack c-text-ellipsis1">{{item.name}}</div>
        </div>
        <div class="c-flex-row c-aligni-center">
          <div class="c-fs20 c-fc-gray c-mr10">{{isWrong?item.hasQuestionNum:item.answerTotal+'/'+item.hasQuestionNum+'道'}}<span class="c-ph10 c-fc-sgray" v-if="chapterInfo.lastRid==item.rId">|</span><span class="c-fs20 theme-fc" v-if="chapterInfo.lastRid==item.rId">上次做到</span></div>
          <div class="c-fs30 c-fc-sgray iconfont">&#xe635;</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { utilJs } from "@/utils/common.js";
export default {
  name: 'PracticeChapter',
  components: {},
  props: {
    practiceId: {
      type: Number,
      default: 0
    },
    isWrong: {
      type: Boolean,
      default: false,
    },
    chapterInfo: {
      type: Object,
      default: function () {
        return {}
      }
    },
    eduflag: {
      type: Boolean,
      default: false,
    },
    ttId: {
      type: String,
      default: '',
    },
    tpId: {
      type: String,
      default: '',
    },
    tlId: {
      type: String,
      default: '',
    },
    progress: {
      type: String,
      default: '',
    },
    isFromPractice: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      cfrom: global.ckFrom
    }
  },
  computed: {
  },
  methods: {
    changeShow(cIndex, item) {
        if (item.hasChildren && !item.children) {
          //获取章节列表/api/itemBank/itemChildTree/{rId}
          let getUrl = this.isWrong ? global.apiurl + "itemBank/itemChildTree/" + item.rId + '?libraryId=' + this.practiceId : global.apiurl + "itemBank/chapterChildList/" + item.rId + '?libraryId=' + this.practiceId
          utilJs.getMethod(getUrl, res => {
            this.$set(this.chapterInfo[cIndex], "children", res)
            this.$set(this.chapterInfo[cIndex], "isShow", true)
          })
      } else {
        if (this.chapterInfo[cIndex].isShow) {
          this.$set(this.chapterInfo[cIndex], "isShow", false)
        } else {
          this.$set(this.chapterInfo[cIndex], "isShow", true)
        }
      }
    },
    chapterPractice(item) {
      if (this.isWrong) {
        if (Number(item.hasQuestionNum) == 0) {
          this.$showCjToast({
            text: "暂无错题需要订正",
            type: "warn",
            time: 1500
          });
          return
        }
        // let queryT = {
        //   wrongNum: item.hasQuestionNum,
        //   libraryId: this.practiceId,
        //   type: 5,
        //   rId: item.rId,
        //   topicName:item.name,
        //   isFromPractice:this.isFromPractice
        // }
        // this.$routerGo(this, "push", {
        //   path: '/member/myWrongTopic/examWrongTopic/itemWrongEnty',
        //   query: queryT
        // })
        this.takePartIn(5, item);
      } else {
        if (Number(item.hasQuestionNum) == 0) {
          this.$showCjToast({
            text: "暂无题目，请联系管理员",
            type: "warn",
            time: 1500
          });
          return
        }
        if (item.testInfo.status == 2) {
          this.$routerGo(this, "push", {
            path: '/homePage/itemBank/questionResult',
            query: {
              examMainId: item.testInfo.examMainId,
              type: 3, //1:顺序练习 2:随机练习
              ttId: this.ttId,
              tpId: this.tpId,
              tlId: this.tlId,
              isFromPractice:this.isFromPractice,
              progress: 100,
            }
          })
        } else if (item.testInfo.status == 1) {
          this.$routerGo(this, "push", {
            path: '/homePage/itemBank/questionAnswer',
            query: {
              libraryId: this.practiceId,
              type: 3,
              rId: item.rId,
              goOnNum: (item.answerTotal + 1) > item.hasQuestionNum ? item.hasQuestionNum : (item.answerTotal + 1),
              ttId: this.ttId,
              tpId: this.tpId,
              tlId: this.tlId,
              progress: this.progress,
              isFromPractice:this.isFromPractice
            }
          })
        } else {
          this.takePartIn(3, item);
        }
      }
    },
    //加入考试
    takePartIn(mode, item) {
      let $this = this;
      let data = {};
      if (mode == 5 || mode == 3) {
        data = {
          libraryId: this.practiceId,
          type: mode,
          rId: item.rId,
        }
      }
      utilJs.postMethod(`${global.apiurl}itemBank/takePartIn`, data, function (
        res
      ) {
        let queryT = {
          libraryId: $this.practiceId,
          type: mode,
          goOnNum: 1,
          ttId: $this.ttId,
          tpId: $this.tpId,
          tlId: $this.tlId,
          progress: $this.progress,
          isFromPractice:$this.isFromPractice
        }
        if (mode == 5 || mode == 3) {
          queryT.rId = item.rId;
        }
        $this.$routerGo($this, "push", {
          path: '/homePage/itemBank/questionAnswer',
          query: queryT
        })
      })
    },
  }
}
</script>
<style scoped>
.fc-vip-up {
  color: #6E442D;
}
.c-pl96{
  padding-left: 2.4rem;
}
</style>


